1 module about_functions_parameters; 2 3 import dunit; 4 import helpers; 5 6 class AboutFunctionParameters { 7 mixin UnitTest; 8 9 10 // parameters are passed by value as default and ALWAYS copied 11 12 void decrement(int value) { 13 value -= 10; 14 } 15 16 @Test 17 public void testParameterByValue() 18 { 19 auto number=100; 20 decrement(number); 21 assertEquals(number,FILL_IN_THIS_NUMBER); 22 } 23 24 25 // ... this is also true for slices 26 // but you can force the reference semantic by adding a "ref" qualifier 27 28 void wrong_append(int[] arr, int elem) { 29 arr ~= elem; // this doesn't work as expected 30 } 31 32 void right_append(ref int[] arr, int elem) { 33 arr ~= elem; // that's better 34 } 35 36 @Test 37 public void testSliceParameter() 38 { 39 40 auto my_array=[4,8,15,16,23]; 41 wrong_append(my_array,42); 42 assertEquals(my_array.length,FILL_IN_THIS_NUMBER); 43 right_append(my_array,42); 44 assertEquals(my_array.length,FILL_IN_THIS_NUMBER); 45 } 46 47 48 // parameters can have "in" and "out" qualifiers, 49 // where "in" stands for input and 50 // "out" for values that will be returned to the caller 51 52 int sumAndProduct(in int n1, in int n2, out int product) { 53 product=n1*n2; 54 return n1+n2; 55 } 56 57 @Test 58 public void exerciseInOutParameters() 59 { 60 int prod; 61 int result = sumAndProduct(7, 3, prod); 62 assertEquals(result, FILL_IN_THIS_NUMBER); 63 assertEquals(prod, FILL_IN_THIS_NUMBER); 64 } 65 66 /*-----------------------------------------*/ 67 68 69 }